package mongodb

import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.extensions.mongodb.Predef._
import io.gatling.extensions.mongodb.protocol._

import scala.concurrent.duration._
import scala.util.Random

class MongoDBTestCase extends Simulation {

  val url: String = "mongodb://root:auto_test%40123@192.168.106.147:27017/admin"

  val protocol: MongoDBProtocol = mongo(url).pollSize(50).waitTime(60)

  val scn_update: ScenarioBuilder = scenario("test_update").forever(
    exec(
      session => {
        session.set("driver_id", Random.nextInt(300000)).set("name", Random.nextInt(300000))
      }
    ).exec(
      mongo("test", "perf_test", "test")
        .updateOne("""{"driver_id":${driver_id}}""", """{"$set":{"name":"${name}"}}""")
        .check(modifiedCount is 1L)
    )
  )

  val scn_insert: ScenarioBuilder = scenario("test_insert").forever(
    exec(
      session => {
        session.set("driver_id", Random.nextInt(300000)).set("name", Random.nextInt(300000))
      }
    ).exec(
      mongo("test", "perf_test", "test_insert")
        .insertOne("""{"driver_id":${driver_id},"name":"${name}"}""")
        .check(wasAcknowledgedOne)
    )
  )

  setUp(
    scn_update.inject(atOnceUsers(1)).protocols(protocol),
    // scn_insert.inject(atOnceUsers(10)).protocols(protocol),
  ).maxDuration(30 days)
}
